package ctrl

import (
	"fmt"
	"log"
	"net/http"

	"gitee.com/cheng470/openai/service"
)

type Ctrl struct {
	fileService  service.FileService
	txAsrService service.TxAsrService
	txTtsService service.TxTtsService
	chatService  service.ChatService
}

func NewController(fileServcie service.FileService, txAsrService service.TxAsrService,
	txTtxService service.TxTtsService, chatService service.ChatService) *Ctrl {
	return &Ctrl{
		fileService:  fileServcie,
		txAsrService: txAsrService,
		txTtsService: txTtxService,
		chatService:  chatService,
	}
}

func (c *Ctrl) Audio2text(w http.ResponseWriter, r *http.Request) {
	r.Body = http.MaxBytesReader(w, r.Body, 3*1024*1024) // 3 Mb
	if err := r.ParseMultipartForm(3 << 20); err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte("文件大于3M"))
		return
	}
	file, handler, err := r.FormFile("file")
	if err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		w.Write([]byte("文件解析失败"))
		return
	}
	defer file.Close()

	// 保存到本地磁盘
	err = c.fileService.SaveToLocal(file, handler.Filename)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		w.Write([]byte("文件保存失败"))
		return
	}

	// 调用腾讯云进行解析
	body, err := c.txAsrService.HandleSentenceRecognition(handler.Filename, r.FormValue("fileFormat"))
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		w.Write([]byte(body))
		return
	}
	w.Write([]byte(body))
}

// 文本转语音
func (c *Ctrl) Text2audio(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	text := r.Form.Get("text")
	if len(text) == 0 {
		log.Println("缺少参数 text")
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte("缺少参数 text"))
		return
	}

	volume := GetParamIntD(r, "volume", 0)
	speed := GetParamIntD(r, "speed", 0)
	voiceType := GetParamIntD(r, "voiceType", 0)
	emotionCategory := GetParamString(r, "emotionCategory")
	emotionIntensity := GetParamIntD(r, "emotionIntensity", 0)

	filePath := c.txTtsService.HandleText2Audio(text, volume, speed, voiceType, emotionCategory, emotionIntensity)
	err := c.fileService.LoadFile(w, filePath)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		w.Write([]byte("文件打开失败"))
		return
	}
}

func (c *Ctrl) HandleDownload(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	filePath := r.Form.Get("file")
	if len(filePath) == 0 {
		log.Println("缺少参数 file")
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte("缺少参数 file"))
		return
	}
	err := c.fileService.LoadFile(w, filePath)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		w.Write([]byte("文件打开失败"))
		return
	}
}

// 聊天接口
func (c *Ctrl) Chat(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	content := r.Form.Get("content")
	characterId := GetParamIntD(r, "characterId", 1)
	name := GetParamStringD(r, "name", "小美")
	if len(content) == 0 {
		fmt.Fprintln(w, "参数不能为空：content")
		return
	}
	if characterId == 1 {
		name = "小美"
	} else if characterId == 2 {
		name = "小丽"
	}
	fmt.Fprintln(w, c.chatService.HandleChat(name, content))
}
